<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSupervisorReviewsTable extends Migration
{
    public function up()
    {
        Schema::create('supervisor_reviews', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('ID');
            $table->date('review_date')->comment('审核日期');
            $table->string('team_name')->comment('班组名称');
            $table->unsignedBigInteger('supervisor_id')->comment('主管/部长ID');
            $table->string('supervisor_name')->comment('主管/部长姓名');
            $table->string('review_status', 20)->default('pending')->comment('审核状态：pending-待审核 approved-正常 rejected-异常');
            $table->text('review_remark')->nullable()->comment('审核备注');
            $table->datetime('reviewed_at')->nullable()->comment('审核提交时间');
            $table->json('leader_review_ids')->nullable()->comment('班长审核ID');

            $table->timestamp('created_at')->nullable()->comment('创建时间');
            $table->timestamp('updated_at')->nullable()->comment('修改时间');
            $table->timestamp('deleted_at')->nullable()->comment('删除时间');

            $table->unique(['team_name', 'review_date', 'supervisor_id', 'deleted_at'], 'uk_team_date_supervisor')->comment('主管每天对每个班组只能审核一次');
            $table->index('review_date', 'idx_review_date')->comment('审核日期索引');
            $table->index('supervisor_id', 'idx_supervisor_id')->comment('主管ID索引');
            $table->index('review_status', 'idx_review_status')->comment('审核状态索引');

            $table->foreign('supervisor_id', 'fk_supervisor_review_supervisor')->references('id')->on('employees')->onDelete('cascade');
        });

        DB::statement('alter table' . ' ' . config('database.connections.mysql.prefix', '') . 'feedbacks comment "主管/部长审核记录表"');

    }

    public function down()
    {
        Schema::dropIfExists('supervisor_reviews');
    }
}

